require "rubygems"
require "activerecord"

module Wondercap
module Deployment
      
class Snapshot < ActiveRecord::Base
  include Enumerable

  has_many :deployments, :dependent => :delete_all
  belongs_to :area
  has_one :current_area, :class_name => "Area", :foreign_key => "current_snapshot_id", :dependent => :nullify
  has_one :unfinished_area, :class_name => "Area", :foreign_key => "unfinished_snapshot_id", :dependent => :nullify

  def assign(snapshot)
    if snapshot
      snapshot.deployments.each do |dep|
        deployments << dep.clone
      end
      attributes = snapshot.attributes.reject { |k,v| k.to_sym == :deployments }
    end
  end

  def add_deployment(dep)
    deployments << dep
  end

  def delete_deployment(dep)
    deployments.delete dep
  end

  def find_deployment_by_name(name)
    deployments.find_by_name name
  end

  def find_or_initialize_deployment_by_name(name)
    deployments.find_or_initialize_by_name name
  end

  def each(&block)
    deployments.each(&block)
  end
end
      
end
end